﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using LTHD_Web.WS;

namespace LTHD_Web.Account
{
    public partial class LoginandRegistery : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ImageButton1_Click(object sender, ImageClickEventArgs e)
        {

        }
        Service1SoapClient ws = new Service1SoapClient();

        protected void bt_login_Click(object sender, EventArgs e)
        {
            string taikhoan = tb_taikhoan.Text.Trim();
            string matkhau = tb_matkhau.Text.Trim();
            if (taikhoan.Equals("") || matkhau.Equals(""))
            {
                lbThongBao.Text="Tài khoản hoặc mật khẩu rỗng";
            }

            if (taikhoan.Length > 1 && matkhau.Length > 1)
            {
                matkhau = MaHoa.Encrypt(matkhau, true);
                TaiKhoanDTO tk = ws.logIn(taikhoan, matkhau);
                if (tk != null)
                {
                    Session.Add("taiKhoan", tk);
                    Session.Timeout = 2400;
                    Response.Cookies["userName"].Value = taikhoan;
                    Response.Cookies["userName"].Expires = DateTime.Now.AddMinutes(40);
                    Response.Cookies["userid"].Value = tk.Id.ToString();
                    Response.Cookies["userid"].Expires = DateTime.Now.AddMinutes(40);

                    HttpCookie aCookie = new HttpCookie("lastVisit");
                    aCookie.Value = DateTime.Now.ToString();
                    aCookie.Expires = DateTime.Now.AddDays(1);
                    Response.Cookies.Add(aCookie);
                    Response.Redirect("Default.aspx");
                }
                else
                {
                    lbThongBao.Text = "Đăng nhập thất bại";
                }
            }
        }

        protected void register_Click(object sender, EventArgs e)
        {
            string taiKhoan = taikhoan.Text.Trim();
            string matKhau = matkhau.Text.Trim();
            dbDataContext db = new dbDataContext();
            taikhoan info = new taikhoan();
            if (taiKhoan.Length > 1 && matKhau.Length > 1)
            {
                info.tentaikhoan = taiKhoan;
                matKhau = MaHoa.Encrypt(matKhau, true);
                info.matkhau = matKhau;
                info.email = email.Text.Trim();
                info.phone = phone.Text.Trim();
                info.diachi = address.Text.Trim();
                info.loaitaikhoan = 2; 
                db.taikhoans.InsertOnSubmit(info); 
            }
            db.SubmitChanges();
            TaiKhoanDTO tk = ws.logIn(taiKhoan, matKhau);
            Session.Add("taiKhoan", taikhoan);
            Response.Cookies["userName"].Value = taiKhoan;
            Response.Cookies["userName"].Expires = DateTime.Now.AddMinutes(40);
            Response.Cookies["userid"].Value = tk.Id.ToString();
            Response.Cookies["userid"].Expires = DateTime.Now.AddMinutes(40);

            HttpCookie aCookie = new HttpCookie("lastVisit");
            aCookie.Value = DateTime.Now.ToString();
            aCookie.Expires = DateTime.Now.AddDays(1);
            Response.Cookies.Add(aCookie);
            Response.Redirect("Default.aspx");

            Response.Redirect("Default.aspx"); 
        }
    }
}